<?php
// 应用公共文件

if (!function_exists('std_result')) {
    function std_result(int $code = 200, string $msg = '', array|object $data = []): array
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
    }
}

if (!function_exists('api_response')) {
    function api_response($d_code = 200, $d_msg = '', $d_data = [], $code = 200, $header = [], $options = [])
    {
        return json(
            std_result($d_code, $d_msg, $d_data),
            $code,
            $header,
            $options
        );
    }
}

if (!function_exists('password_md5')) {
    function password_md5($password, $salt)
    {
        return md5(sha1($password) . $salt);
    }
}

if (!function_exists('is_admin_login')) {
    function is_admin_login()
    {
        $request = request();
        $admin_auth = \app\library\api\admin\Auth::instance();
        $re_jwt = $admin_auth->decodeJWT($admin_auth->getAuthHeader($request));
        if ($re_jwt['code'] != 200) {
            return false;
        } else {
            $x_token_user = $re_jwt['data']['data'];
            if (isset($x_token_user['from']) && $x_token_user['from'] == '1') {
                return $x_token_user['id'];
            } else {
                return false;
            }
        }
    }
}

if (!function_exists('is_login')) {
    function is_login()
    {
        $request = request();
        $admin_auth = \app\library\api\admin\Auth::instance();
        $re_jwt = $admin_auth->decodeJWT($admin_auth->getAuthHeader($request));
        if ($re_jwt['code'] != 200) {
            return false;
        } else {
            $x_token_user = $re_jwt['data']['data'];
            if (isset($x_token_user['from']) && $x_token_user['from'] == '2') {
                return $x_token_user['id'];
            } else {
                return false;
            }
        }
    }
}

if (!function_exists('is_super_admin')) {
    function is_super_admin($uid = null)
    {
        $uid = $uid === null ? is_admin_login() : $uid;
        return $uid && (intval($uid) === intval(config('zhtp.super_admin_id')));
    }
}


if (!function_exists('list_to_tree')) {
    /**
     * 把返回的数据集转换成Tree
     * @param array $list 要转换的数据集
     * @param string $pid parent标记字段
     * @param string $level level标记字段
     * @return array
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = &$refer[$parentId];
                        $parent[$child][] = &$list[$key];
                    }
                }
            }
        }
        return $tree;
    }
}

if (!function_exists('desensitization')) {
    /**
     * 数据脱敏（只数组，根据数组 key 脱敏）
     * @param array|string $data 待脱敏的数据
     * @param array $customRegex 自定义的脱敏正则表达式数组，默认为空数组
     * @return array|string
     */
    function desensitization(array|string $data, array $customRegex = []): array|string
    {
        // 合并默认正则表达式和自定义正则表达式
        $desensitizationRegex = array_merge([
            '/(password|salt|token)/i'
        ], $customRegex);

        if (!is_array($data) || !$desensitizationRegex) {
            return $data;
        }
        foreach ($data as $index => &$item) {
            foreach ($desensitizationRegex as $reg) {
                if (preg_match($reg, strval($index))) {
                    $item = "***";
                } elseif (is_array($item)) {
                    $item = desensitization($item, $customRegex);
                }
            }
        }
        return $data;
    }
}
